package android.support.v7.widget;


import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * 可水平自动折行的LayoutManager
 * TODO 2017/03/13最终要想LinearLayoutManager那样健壮
 * Created by ldkxingzhe@163.com on 17/3/13.
 */
public class AutoLineLayoutManager extends RecyclerView.LayoutManager{
    @SuppressWarnings("unused")
    private static final String TAG = "AutoLineLayoutManager";


    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        detachAndScrapAttachedViews(recycler);

        int sumWidth = getWidth();
        int curLineWidth = 0, curLineTop = 0;
        int lastLineMaxHeight = 0;
        for (int i = 0; i < getItemCount(); i++){
            View view = recycler.getViewForPosition(i);
            addView(view);
            measureChildWithMargins(view, 0, 0);
            int width = getDecoratedMeasuredWidth(view);
            int height = getDecoratedMeasuredHeight(view);

            curLineWidth += width;
            if (curLineWidth <= sumWidth){
                layoutDecorated(view, curLineWidth - width, curLineTop, curLineWidth, curLineTop + height);
                lastLineMaxHeight = Math.max(lastLineMaxHeight, height);
            }else{
                curLineWidth = width;
                if (lastLineMaxHeight == 0){
                    lastLineMaxHeight = height;
                }
                curLineTop += lastLineMaxHeight;
                layoutDecorated(view, 0, curLineTop, width, curLineTop + height);
                lastLineMaxHeight = height;
            }
        }
    }


    @Override
    public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
        int width = View.MeasureSpec.getSize(widthSpec);
//        Log.d(TAG, "width is " + width);
        int curLineWidth = 0, curLineTop = 0;
        int lastLineMaxHeight = 0;
        detachAndScrapAttachedViews(recycler);
        int itemWidthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.AT_MOST);
        int itemHeightSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.UNSPECIFIED);
        for (int i = 0; i < getItemCount(); i++){
            View childView = recycler.getViewForPosition(i);
            childView.measure(itemWidthSpec, itemHeightSpec);
            int childWidth = getDecoratedMeasuredWidth(childView);
            int childHeight = getDecoratedMeasuredHeight(childView);
//            Log.d(TAG, "childView.getMeasureHeight is : " + childView.getMeasuredHeight());
            curLineWidth += childWidth;
            if (curLineWidth <= width){
                if (childHeight > lastLineMaxHeight){
                    lastLineMaxHeight = childHeight;
                }
            }else{
                curLineWidth = childWidth;
                if (lastLineMaxHeight == 0){
                    lastLineMaxHeight = childHeight;
                }
                curLineTop += lastLineMaxHeight;
                lastLineMaxHeight = childHeight;
            }
//            Log.d(TAG, "curLineTop is " + curLineTop + ", childHeight is " + childHeight);
        }
        int curLineBottom = curLineTop + lastLineMaxHeight;
//        Log.d(TAG, "curLineBottom is " + curLineBottom + ", lastLineMaxHeight is " + lastLineMaxHeight);
        int resultHeightSpec = View.MeasureSpec.makeMeasureSpec(curLineBottom, View.MeasureSpec.EXACTLY);
        final int resultWidthSpec = RecyclerView.LayoutManager.chooseSize(widthSpec,
                getPaddingLeft() + getPaddingRight(),
                0);
        setMeasuredDimension(resultWidthSpec, resultHeightSpec);
    }
}
